// Copyright (C) 2024 Kumo inc.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
//

#include <kllm/kai/rerank.h>

namespace kllm {

    void KaiRerank::rank(const KaiRequest &req, KaiResponse&res) {
        res.set_model_alias(_context->params.model_alias);
        std::vector<server_task> tasks = _context->create_tasks_inference(req, SERVER_TASK_INF_TYPE_RERANK, req.rerank_request().prompts());
        _context->queue_results.add_waiting_tasks(tasks);
        _context->queue_tasks.post(tasks);

        // get the result
        std::unordered_set<int> task_ids = server_task::get_list_id(tasks);

        _context->receive_cmpl_results(task_ids, [&](std::vector<TaskResult> &results) {
            for (const auto &it: results) {
                *res.mutable_reranks()->Add() = it.reranks;
            }
        }, [&](const turbo::Status &status) {
            res.mutable_status()->set_code(status.raw_code());
            res.mutable_status()->set_errmsg(std::string(status.message()));
        });

        _context->queue_results.remove_waiting_task_ids(task_ids);
    }
}  // namespace kllm
